import scrapy
from scrapy import Request

from jayce.items import Ssr1Item


class Ssr1Spider(scrapy.Spider):
    name = 'ssr1'
    allowed_domains = ['ssr1.scrape.center']
    start_urls = ['https://ssr1.scrape.center/']

    def start_requests(self):
        for url in self.start_urls:
            yield Request(url=url, callback=self.parse_item)

    def parse_item(self, response):
        links = [response.urljoin(link.extract()) for link in
                 response.xpath("//*[@id='index']/div[1]/div/div/div/div/div[2]/a/@href")]
        for link in links:
            yield Request(url=link, callback=self.parse)
        # 翻页
        next_url = response.xpath("//a[@class='next']//@href")[0].extract()
        if next_url is not None:
            yield Request(url=response.urljoin(next_url), callback=self.parse_item)

    def parse(self, response):
        film_name = response.xpath('//*[@id="detail"]/div[1]/div/div/div[1]/div/div[2]/a/h2/text()')[0].extract()
        film_types = [t.extract() for t in response.xpath("//div[@class='categories']//span/text()")]
        span_texts = [t.extract() for t in
                      response.xpath("//*[@id='detail']/div[1]/div/div/div[1]/div/div[2]/div[2]/span/text()")]
        film_country = span_texts[0]
        film_duration = span_texts[2].strip()[:-3]
        film_show_time = response.xpath('//*[@id="detail"]/div[1]/div/div/div[1]/div/div[2]/div[3]/span/text()')
        if len(film_show_time) > 0:
            film_show_time = film_show_time[0].extract()[:-3].strip()
        else:
            film_show_time = ''
        film_synopsis = response.xpath('//*[@id="detail"]/div[1]/div/div/div[1]/div/div[2]/div[4]/p/text()')[
            0].extract().strip()
        film_director = response.xpath('//*[@id="detail"]/div[2]/div/div/div/div/div/p/text()')[
            0].extract()
        film_score = response.xpath('//*[@id="detail"]/div[1]/div/div/div[1]/div/div[3]//p/text()')[
            0].extract().strip()
        yield Ssr1Item(film_name=film_name,
                       film_types=film_types,
                       film_country=film_country,
                       film_duration=film_duration,
                       film_show_time=film_show_time,
                       film_synopsis=film_synopsis,
                       film_director=film_director,
                       film_score=film_score)
